package com.xinan.blog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xinan.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * BlogController
 * 注意：在实际项目中业务与sql需要写在Model中，此demo仅为示意,故将sql写在了Controller中
 */
@Before(BlogInterceptor.class)
public class BlogController extends BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());
	public void index() {
		render("/WEB-INF/views/blog/blog.html");
	}
	
	public void query() {
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("rows"); 
		Page<Blog> page = Blog.dao.page(pageNumber, pageSize);
		Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
//		root.put("page", page.getPageNumber());
//		root.put("totalPage", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
	}
	
	public void save() {
		getModel(Blog.class).save();
		renderJson(Constants.SAVESUCCESS);
	}
	
	public void update() {
		getModel(Blog.class).update();
		renderJson(Constants.EDITSUCCESS);
	}
	
	public void delete() {
		Blog.dao.deleteById(getParaToInt());
		renderJson(Constants.DELETESUCCESS);
	}
}


